// pages/test/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexnum: 0,
    chkid:0,
    movielist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var movielist = [
      {
        id: 1,
        name: '红海行动',
        imgpath: '/resources/tu1.jpg',
        ischk: true,
        pingfen: '评分:8.0',
        shichang: '时长:150分钟'
      },
      {
        id: 2,
        name: '唐人街探案2',
        imgpath: '/resources/tu2.jpg',
        ischk: false,
        pingfen: '评分:7.2',
        shichang: '时长:120分钟'
      },
      {
        id: 3,
        name: '捉妖记2',
        imgpath: '/resources/tu3.jpg',
        ischk: false,
        pingfen: '评分:5.2',
        shichang: '时长:120分钟'
      },
      {
        id: 4,
        name: '唐人街探案2',
        imgpath: '/resources/tu4.jpg',
        ischk: false,
        pingfen: '评分:7.2',
        shichang: '时长:120分钟'
      },
      {
        id: 5,
        name: '捉妖记2',
        imgpath: '/resources/tu1.jpg',
        ischk: false,
        pingfen: '评分:5.2',
        shichang: '时长:120分钟'
      },
      {
        id: 6,
        name: '红海行动',
        imgpath: '/resources/tu1.jpg',
        ischk: true,
        pingfen: '评分:8.0',
        shichang: '时长:150分钟'
      },
      {
        id: 7,
        name: '唐人街探案2',
        imgpath: '/resources/tu2.jpg',
        ischk: false,
        pingfen: '评分:7.2',
        shichang: '时长:120分钟'
      },
      {
        id: 8,
        name: '捉妖记2',
        imgpath: '/resources/tu3.jpg',
        ischk: false,
        pingfen: '评分:5.2',
        shichang: '时长:120分钟'
      },
      {
        id: 9,
        name: '唐人街探案2',
        imgpath: '/resources/tu4.jpg',
        ischk: false,
        pingfen: '评分:7.2',
        shichang: '时长:120分钟'
      },
      {
        id: 10,
        name: '捉妖记2',
        imgpath: '/resources/tu1.jpg',
        ischk: false,
        pingfen: '评分:5.2',
        shichang: '时长:120分钟'
      },
    ];

    that.setData({
      movielist: movielist
    })
  },
  
  /**
   * 滑动事件
   * **/
  scroll: function (e) {
    console.log("滑动事件:");
    console.log(e);
    var that = this;

    var movielist = that.data.movielist;    
    var indexnum = that.data.indexnum;
    //向右deltaX>0  向左侧deltaX<0 
    //scrollLeft判断滑动的宽度，获取选中的值
    //向左滑动
    if (e.detail.deltaX<0)
    {
        var scrollnum = parseInt(e.detail.scrollLeft / 40);
        indexnum += scrollnum;
        console.log("向左滑动AA：" + indexnum)
        if (indexnum <movielist.length)
        {
          that.setData({
            chkid: movielist[indexnum].id,
            indexnum: indexnum
          })
        }else{
          that.setData({
            chkid: movielist[movielist.length - 1].id,
            indexnum: movielist.length-1
          })
        }   
    }//向右滑动
    else if(e.detail.deltaX >0)
    {     
      var scrollnum = parseInt(e.detail.scrollLeft / 40);
      indexnum -= scrollnum;
      console.log("向右滑动AA：" + indexnum)
      if (indexnum <0) 
      {
        that.setData({
          chkid: movielist[0].id,
          indexnum: 0
        })
      } else {
        that.setData({
          chkid: movielist[indexnum].id,
          indexnum: indexnum
        })
      }   
    }
    that.getchkmovie();
  },
  //得到电影
  getchkmovie: function () {
    var that = this;
    var movielist = that.data.movielist;

    var txtarry = [];
    for (var j = 0; j < movielist.length; j++) {
      if (that.data.chkid == movielist[j].id) {
        txtarry[j] = {
          id: movielist[j].id,
          name: movielist[j].name,
          imgpath: movielist[j].imgpath,
          ischk: true,
          pingfen: movielist[j].pingfen,
          shichang: movielist[j].shichang
        }
      } else {
        txtarry[j] = {
          id: movielist[j].id,
          name: movielist[j].name,
          imgpath: movielist[j].imgpath,
          ischk: false,
          pingfen: movielist[j].pingfen,
          shichang: movielist[j].shichang
        }
      }
    }
    console.log("重置后的电影的数据:")
    console.log(txtarry)
    that.setData({
      movielist: txtarry
    })
    //重置数据
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})